export default{
	state: {
		hasLogin: uni.getStorageSync('uni_id_token') && uni.getStorageSync('uni_id_token_expired') > Date.now(),
		role: uni.getStorageSync('role'),
		userInfo:uni.getStorageSync('userInfo')||false
	},
	getters:{
		isMember:(state)=> {
			if(!state.role) return false
			let index = state.role.findIndex(v => v.role_name === "student")
			return index === -1 ? false : true
		},
		isTeamLeader:(state)=> {
			if(!state.role) return false
			let index = state.role.findIndex(v => v.role_name === "leader")
			return index === -1 ? false : true
		},
		isAdmin:(state)=>{
			if(!state.role) return false
			let index =state.role.findIndex(v => v.role_name === "admin")
			return index === -1 ? false : true
		}
	},
	mutations: {
		vModel(state,{key,value}){
			state[key]=value
			if(key==='userInfo'||key==='role'){
				uni.setStorageSync(key, value)
			}
		},
		initRole(state,{key,value}){
			state[key]=value
			uni.setStorageSync(key, value)
		},
		login(state, {
			token,
			tokenExpired,
			userinfo,
			role
		}) {
			if(!userinfo.avatarUrl){
				userinfo.avatarUrl="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-aliyun-kllkkfqzblxy94fd50/cd5ef650-261a-11eb-b680-7980c8a877b8.jpg"
			}
			uni.setStorageSync('uni_id_token', token)
			uni.setStorageSync('uni_id_token_expired', tokenExpired)
			uni.setStorageSync('userInfo', userinfo)
			uni.setStorageSync('role', role)
			
			state.hasLogin = true
			state.userInfo = userinfo
			state.role = role
			state.uni_id_token=token
		},
		updateUserInfo(state,userinfo){
			state.userInfo = userinfo
			uni.setStorageSync('userInfo', userinfo)
		},
		logout(state) {
			uni.removeStorageSync('uni_id_token')
			uni.removeStorageSync('uni_id_token_expired')
			uni.removeStorageSync('userInfo')
			uni.removeStorageSync('role')
			uni.removeStorageSync('teacher')
			uni.removeStorageSync('student')
			state.hasLogin = false
			state.userInfo = null
			state.role = null
		},
	},
	actions:{
		initLogin({state}){
			if(state.hasLogin!==true){
				// uni.redirectTo({
				// 	url:'/pages/password/password'
				// })
				// uni.redirectTo({
				// 	url:'/pages/login/login'
				// })
			}
		}
	}
}